package com.app.bimo.module_bookrack.mvp.persenter;


import android.graphics.Rect;
import android.util.Log;
import android.view.View;


import com.app.bimo.commonbusiness.mvp.BasePresenter;
import com.app.bimo.commonbusiness.network.RxObservableUtil;
import com.app.bimo.commonbusiness.util.SystemUtil;
import com.app.bimo.library_db.BookData;
import com.app.bimo.library_db.ReadTimeRecord;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.helper.BookHelper;
import com.app.bimo.library_db.helper.ReadDayRecordHelper;
import com.app.bimo.library_db.helper.UserHelper;
import com.app.bimo.module_bookrack.mvp.constract.BookrackContract;
import com.app.bimo.module_bookrack.mvp.model.entiy.OtherResult;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BookrackPresenter extends BasePresenter<BookrackContract.Model, BookrackContract.View> {
    public BookrackPresenter(BookrackContract.Model model, BookrackContract.View rootView) {
        super(model, rootView);
    }

    public void addObservable(List<BookData> chacthList) {
        toObservableSticky(ReadTimeRecord.class, readTimeRecord -> {
            Log.e("aaa", "addObservable: ");
            getBooks(1);
            readTimeRequestFromModel();
        });

    }

    /**
     * 获取书架列表
     * @param page   获取的页码
     */
    public void getBooks(int page) {
        String uuid = "";
        UserData userData = UserHelper.getsInstance().findUser();
        if (userData != null) {
            uuid = userData.getUuid();
        }
        String finalUuid = uuid;
        RxObservableUtil.subscribe(
                mModel.getBooks(page + "")
                , bindCustomLifecycle(), bookStoreDataList -> {
                        mRootView.bookrackDataNotify(bookStoreDataList, page, true);
                        //待补充
                });
    }

    /**
     * 判断本地数据库里的数据和网络数据是否一样
     *
     * @param dbList
     * @param netList
     * @return
     */
    private boolean isChange(List<BookData> dbList, List<BookData> netList) {
        if (dbList.size() != netList.size()) {
            return true;
        }
        for (int i = 0; i < dbList.size(); i++) {
            if (!dbList.get(i).equals(netList.get(i))) {
                return true;
            }

        }
        return false;
    }

    /***
     * 不管有没有存数据库 都把书籍拿出来放进数据库
     * 如果有登录  添加当前用户数据进去
     * @param list
     * @return
     */
    private void test(List<BookData> list) {
        UserData userData = UserHelper.getsInstance().findUser();
        if (userData == null) {
            return;
        }
        List<BookData> tempList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BookData data = BookHelper.getsInstance().findBookByUserIdAndNovelId(userData.getUuid(), list.get(i).getNovelid());
            if (data == null) {
                data = list.get(i);
            }
            data.setInBookself(1);
            data.setOrderTime(i);
            data.setUuid(userData.getUuid());
            tempList.add(data);
        }
        BookHelper.getsInstance().saveBooksToBookRack(tempList);
    }


    /**
     * 周阅读时长
     */
    public void readTimeRequestFromModel() {
        RxObservableUtil.subscribe(
                mModel.weekReadTime()
                , bindCustomLifecycle(), bookStoreDataList -> {
                    long time = ReadDayRecordHelper.getsInstance().findWeekTime();
                    if (time == 0) {
                        mRootView.weekReadTimeNotify(bookStoreDataList);
                    } else {
                        if (bookStoreDataList.getReadTime() < time) {
                            bookStoreDataList.setReadTime(time);
                        }
                        mRootView.weekReadTimeNotify(bookStoreDataList);
                    }
                }, e -> {
                    OtherResult data = new OtherResult();
                    long time = ReadDayRecordHelper.getsInstance().findWeekTime();
                    data.setReadTime(time);
                    mRootView.weekReadTimeNotify(data);
                });
    }

    /**
     * 推荐阅读
     */
    public void getRecommend() {
        RxObservableUtil.subscribe(
                mModel.getBookshelfRecommend()
                , bindCustomLifecycle(), bookStoreDataList -> {
                    // 处理数据 直接获取到List<JavaBean> carBeans
                    mRootView.getBookshelfRecommendNotify(bookStoreDataList);
                }, e -> {

                });
    }

    /**
     * 公告
     */
    public void getAnnounce() {
        Log.i("getAnnounce",Log.getStackTraceString(new Throwable()));
        RxObservableUtil.subscribe(
                mModel.getAnnounce()
                , bindCustomLifecycle(), bookStoreDataList -> {
                    // 处理数据 直接获取到List<JavaBean> carBeans
                    mRootView.getAnnounceNotify(bookStoreDataList);
                }, e -> {

                });
    }

    /**
     * 头部背景广告
     */
    public void getBanner() {
        RxObservableUtil.subscribe(
                mModel.getBanner(6)
                , bindCustomLifecycle(), bookStoreDataList -> {
                    // 处理数据 直接获取到List<JavaBean> carBeans
                    mRootView.getBannerNotify(bookStoreDataList);
                }, e -> {

                });
    }


    /**
     * 计算RecyclerView.ItemDecoration
     * @return 计算RecyclerView.ItemDecoration
     */
    public RecyclerView.ItemDecoration div() {

        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int spanCount = 3;
                int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition() - 1;
                if (itemPosition < 0) {
                    return;
                }
                int left;
                int right;
                if (itemPosition % spanCount == 0) {
                    left = SystemUtil.dip2px(view.getContext(), 8);
                    right = 0;
                } else if (itemPosition % spanCount == 1) {
                    left = SystemUtil.dip2px(view.getContext(), 0);
                    right = SystemUtil.dip2px(view.getContext(), 0);
                } else {
                    left = 0;
                    right = SystemUtil.dip2px(view.getContext(), 8);
                }
                if (parent.getChildPosition(view) >= 1 && parent.getChildPosition(view) <= 3){
                    outRect.set(left, SystemUtil.dip2px(view.getContext(), 6), right, SystemUtil.dip2px(view.getContext(), 0));
                }else{
                    outRect.set(left, SystemUtil.dip2px(view.getContext(), 12), right, SystemUtil.dip2px(view.getContext(), 0));
                }
            }
        };

    }
}
